<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8" />
    <title>用户登录</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <style>
        body {
            font-family: "Segoe UI", sans-serif;
            background-color: #f4f6f9;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            margin: 0;
        }

        .login-container {
            background-color: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            width: 100%;
            max-width: 400px;
        }

        .login-container h2 {
            text-align: center;
            margin-bottom: 20px;
        }

        .form-group {
            margin-bottom: 15px;
        }

        label {
            display: block;
            font-weight: bold;
            margin-bottom: 5px;
        }

        input[type="text"],
        input[type="password"] {
            width: 100%;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 4px;
        }

        button {
            width: 100%;
            padding: 10px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 4px;
            font-size: 16px;
            cursor: pointer;
        }

        button:hover {
            background-color: #0056b3;
        }

        .tip {
            text-align: center;
            margin-top: 15px;
            color: #666;
        }

        .tip a {
            color: #007bff;
            text-decoration: none;
        }

        .tip a:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>

<div class="login-container">
    <h2>用户登录</h2>
    <div class="form-group">
        <label for="userName">用户名：</label>
        <input type="text" id="userName" placeholder="请输入用户名" required />
    </div>
    <div class="form-group">
        <label for="password">密码：</label>
        <input type="password" id="password" placeholder="请输入密码" required />
    </div>
    <button onclick="submitLogin()">立即登录</button>
    <div class="tip">
        没有账号？<a href="zhuce.html">点击注册</a>
    </div>
</div>

<script>
    function submitLogin() {
        const userName = $('#userName').val().trim();
        const password = $('#password').val().trim();

        if (!userName || !password) {
            alert("用户名或密码不能为空");
            return;
        }

        $.ajax({
            url: '/api/users/login',
            method: 'POST',
            contentType: 'application/json',
            data: JSON.stringify({
                userName: userName,
                password: password
            }),
            success: function(response) {
                if (response.success) {
                    alert("登录成功！");
                    window.location.href = "index.html"; // 跳转主页
                } else {
                    alert(response.message || "登录失败，请检查用户名或密码");
                }
            },
            error: function(xhr, status, error) {
                alert("请求失败：" + xhr.responseText);
            }
        });
    }
</script>

</body>
</html>